<template>
  <a-modal v-model="visible" title="参数设置" @ok="handleOk" @cancel="handleCancel" :width="width">
    <a-row>
      <a-col span=6>
        主电压最大值<a-input :placeholder="placeholders[0]" v-model="myDevice.baseinfo.mainVolMax" />
      </a-col>
      <a-col span=6 style="padding-left:10px;">
        主电压最小值<a-input :placeholder="placeholders[1]" v-model="myDevice.baseinfo.mainVolMin" />
      </a-col>
      <a-col span=6 style="padding-left:10px;">
        总电流最大值<a-input :placeholder="placeholders[2]" v-model="myDevice.baseinfo.mainCurMax" />
      </a-col>
      <a-col span=6 style="padding-left:10px;">
        备电压最大值<a-input :placeholder="placeholders[3]" v-model="myDevice.baseinfo.backVolMax" />
      </a-col>
      <a-col span=6 style="padding-top: 18px;">
        备电压最小值<a-input :placeholder="placeholders[4]" v-model="myDevice.baseinfo.backVolMin" />
      </a-col>
      <a-col span=6 style="padding-left:10px; padding-top: 18px;">
        温度最大值<a-input :placeholder="placeholders[5]" v-model="myDevice.baseinfo.tempMax" />
      </a-col>
      <a-col span=6 style="padding-left:10px; padding-top: 18px;">
        风扇开启温度<a-input :placeholder="placeholders[6]" v-model="myDevice.baseinfo.fanTempMax" />
      </a-col>
    </a-row>
    <span>设置说明:</span>
    <template v-for="(item,index) in placeholders">
      <a-row style="margin-top: 5px;">{{ (index + 1) + "." + item }}</a-row>
    </template>
  </a-modal>
</template>

<script>
import { setDevice } from '@/api/deviceApi'

export default {
  name: 'TigDeviceConfigSetModal',
  components: {

  },
  props: {

  },
  data() {
    return {
      visible: false,
      width: '800px',
      myDevice: {
        baseinfo: {},
        network: {},
        warning: {},
        switches: {
          total: 0,
          switches: []
        },
      },
      limit: [],
      placeholders: [],
    }
  },
  computed: {

  },
  methods: {
    handleOk() {
      var that = this
      if (this.myDevice.baseinfo.mainVolMax == '' || this.myDevice.baseinfo.mainVolMin == '' ||
        this.myDevice.baseinfo.mainCurMax == '' || this.myDevice.baseinfo.backVolMax == '' ||
        this.myDevice.baseinfo.backVolMin == '' || this.myDevice.baseinfo.tempMax == '' ||
        this.myDevice.baseinfo.fanTempMax == '') {
        this.$message.info("请设置好所有预警值")
        return
      }
      if (!this.checkSettingLimit())
        return
      setDevice({ cmd: 'setDeviceConfig', data: this.myDevice }).then(res => {
        that.$message.info("设置已发送")
      })
      this.visible = false
    },
    handleCancel() {
      this.visible = false
    },
    show() {
      this.visible = true
    },
    edit(device) {
      //console.log("==========edit device ============" + JSON.stringify(device));
      this.myDevice = JSON.parse(JSON.stringify(device));
      //console.log("==========edit myDevice ============" + JSON.stringify(myDevice));
      this.limit = this.myDevice.limit;
      this.initData();
      console.log("------configsetmodel:edit:" + JSON.stringify(this.myDevice));
      this.show()
    },
    hide() {
      this.visible = false
    },
    checkSettingLimit() {
      if (this.myDevice.baseinfo.mainVolMax > this.limit.mainVolMaxLimt[1] || this.myDevice.baseinfo.mainVolMax < this.limit.mainVolMaxLimt[0]) {
        this.$message.warning("主电压最大值设置超出范围");
        return false

      }
      if (this.myDevice.baseinfo.mainVolMin > this.limit.mainVolMinLimt[1] || this.myDevice.baseinfo.mainVolMin < this.limit.mainVolMinLimt[0]) {
        this.$message.warning("主电压最小值设置超出范围");
        return false

      }
      if (this.myDevice.baseinfo.backVolMax > this.limit.backVolMaxLimt[1] || this.myDevice.baseinfo.backVolMax < this.limit.backVolMaxLimt[0]) {
        this.$message.warning("备电压最大值设置超出范围");
        return false

      }
      if (this.myDevice.baseinfo.backVolMin > this.limit.backVolMinLimt[1] || this.myDevice.baseinfo.backVolMin < this.limit.backVolMinLimt[0]) {
        this.$message.warning("备电压最小值设置超出范围");
        return false

      }
      if (this.myDevice.baseinfo.tempMax > this.limit.tempLimt[1] || this.myDevice.baseinfo.tempMax < this.limit.tempLimt[0]) {
        this.$message.warning("温度设置超出范围");
        return false

      }
      if (this.myDevice.baseinfo.mainCurMax > this.limit.mainCurLimt[1] || this.myDevice.baseinfo.mainCurMax < this.limit.mainCurLimt[0]) {
        this.$message.warning("总电流设置超出范围");
        return false

      }
      if (this.myDevice.baseinfo.fanTempMax > this.limit.fanTempLimt[1] || this.myDevice.baseinfo.fanTempMax < this.limit.fanTempLimt[0]) {
        this.$message.warning("风扇开启温度设置超出范围");
        return false

      }
      return true;
    },
    initData() {
      this.placeholders = [];
      this.placeholders.push("主电压最大值" + this.limit.mainVolMaxLimt[0] + '-' + this.limit.mainVolMaxLimt[1] + ",默认值:" + this.limit.mainVolMaxLimt[2]);
      this.placeholders.push("主电压最小值" + this.limit.mainVolMinLimt[0] + '-' + this.limit.mainVolMinLimt[1] + "默认值:" + this.limit.mainVolMinLimt[2]);
      this.placeholders.push("总电流最大值" + this.limit.mainCurLimt[0] + '-' + this.limit.mainCurLimt[1] + "默认值:" + this.limit.mainCurLimt[2]);
      this.placeholders.push("备电压最大值" + this.limit.backVolMaxLimt[0] + '-' + this.limit.backVolMaxLimt[1] + "默认值:" + this.limit.backVolMaxLimt[2]);
      this.placeholders.push("备电压最小值" + this.limit.backVolMinLimt[0] + '-' + this.limit.backVolMinLimt[1] + "默认值:" + this.limit.backVolMinLimt[2]);
      this.placeholders.push("温度最大值" + this.limit.tempLimt[0] + '-' + this.limit.tempLimt[1] + "默认值:" + this.limit.tempLimt[2]);
      this.placeholders.push("风扇开启温度" + this.limit.fanTempLimt[0] + '-' + this.limit.fanTempLimt[1] + "默认值:" + this.limit.fanTempLimt[2]);

      //对后端DecimalFormat类型数据处理一下，否则会显示.01类似的数据
      if (this.myDevice.baseinfo.mainVolMax != undefined && this.myDevice.baseinfo.mainVolMax != "") {
        this.myDevice.baseinfo.mainVolMax = this.myDevice.baseinfo.mainVolMax * 10 / 10;
      }
      if (this.myDevice.baseinfo.mainVolMin != undefined && this.myDevice.baseinfo.mainVolMin != "") {
        this.myDevice.baseinfo.mainVolMin = this.myDevice.baseinfo.mainVolMin * 10 / 10;
      }
      if (this.myDevice.baseinfo.mainCurMax != undefined && this.myDevice.baseinfo.mainCurMax != "") {
        this.myDevice.baseinfo.mainCurMax = this.myDevice.baseinfo.mainCurMax * 10 / 10;
      }
      if (this.myDevice.baseinfo.backVolMax != undefined && this.myDevice.baseinfo.backVolMax != "") {
        this.myDevice.baseinfo.backVolMax = this.myDevice.baseinfo.backVolMax * 10 / 10;
      }
      if (this.myDevice.baseinfo.backVolMin != undefined && this.myDevice.baseinfo.backVolMin != "") {
        this.myDevice.baseinfo.backVolMin = this.myDevice.baseinfo.backVolMin * 10 / 10;
      }
      if (this.myDevice.baseinfo.tempMax != undefined && this.myDevice.baseinfo.tempMax != "") {
        this.myDevice.baseinfo.tempMax = this.myDevice.baseinfo.tempMax * 10 / 10;
      }
      if (this.myDevice.baseinfo.fanTempMax != undefined && this.myDevice.baseinfo.fanTempMax != "") {
        this.myDevice.baseinfo.fanTempMax = this.myDevice.baseinfo.fanTempMax * 10 / 10;
      }
    }
  },
  watch: {

  },
}
</script>